<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class ProyectRequirementsController extends AppController {

    var $name = "ProyectRequirements";

    function edit($id = null) {
        $this->set('est', $this->Session->read('estado'));
        if (empty($this->data)) {
            App::import('Model', 'RequirementFault');
            $ava = new RequirementFault();
            $ava->recursive = -1;
            $this->ProyectRequirement->recursive = 0;
            $this->data = $this->ProyectRequirement->find('first', array('conditions' => array('ProyectRequirement.id' => $id), 'fields' => array('ProyectRequirement.id', 'ProyectRequirement.calificacion', 'ProyectRequirement.requirement_id', 'Proyect.codigo', 'Requirement.texto')));
            $this->set('chequeados', $this->ProyectRequirement->ProyectRequirementsRequirementFault->findAllByProyectRequirementId($id));
            $this->set('condiciones', $ava->find('all', array('conditions' => array('RequirementFault.requirement_id' => $this->data['ProyectRequirement']['requirement_id']), 'order' => array('RequirementFault.tipo' => 'asc'), 'fields' => array('RequirementFault.id', 'RequirementFault.nombre', 'RequirementFault.tipo'))));
        } else {


            $datos = $this->params['form'];
            if ($this->ProyectRequirement->save($this->data)) {
                $ide = $this->data['ProyectRequirement']['id'];
                $this->ProyectRequirement->ProyectRequirementsRequirementFault->deleteAll(array('ProyectRequirementsRequirementFault.proyect_requirement_id' => $ide));
                $flag = 0;
                foreach ($datos['checked'] as $value) {
                    if ($value != 0) {
                        $flag = 1;
                        $this->ProyectRequirement->ProyectRequirementsRequirementFault->query("INSERT INTO proyect_requirements_requirement_faults (proyect_requirement_id,requirement_fault_id) values($ide,$value)");
                    }
                }
                if ($flag == 0) {
                    $this->ProyectRequirement->updateAll(array('ProyectRequirement.calificacion' => 0), array('ProyectRequirement.id' => $ide));
                }



                $this->Session->setFlash('Registro guardado exitosamente');
                $this->redirect(array('controller' => 'Requirements', 'action' => 'social_index'));
            }
        }
    }

    function edit_correction($id) {
        $this->set('est', $this->Session->read('estado'));
        if (empty($this->data)) {
            $this->data = $this->ProyectRequirement->find('first', array('conditions' => array('ProyectRequirement.id' => $id)));
        } else {



            if ($this->ProyectRequirement->save($this->data)) {
                $this->Session->setFlash('Registro guardado exitosamente');
                $this->redirect(array('controller' => 'CorrectionLetters', 'action' => 'index'));
            }
        }
    }

    function reevaluation_index($claim_id) {
        $this->set('claim_id', $claim_id);
        $this->ProyectRequirement->Requirement->recursive = -1;
        $requerimientos = $this->ProyectRequirement->Requirement->find(
                'all', array(
            'conditions' => array('Requirement.requirement_type_id' => 1),
            'fields' => array('Requirement.id')
                ));

        $proyecto = $this->Session->read('proy_id');
        foreach ($requerimientos as $req) {
            $r = $req['Requirement']['id'];
            $cont = $this->ProyectRequirement->find('count', array('conditions' => array('requirement_id' => $req['Requirement']['id'], 'proyect_id' => $proyecto, 'claim_id' => $claim_id)));
            if ($cont == 0) {
                $this->ProyectRequirement->query("INSERT INTO proyect_requirements (proyect_id,requirement_id,claim_id)values($proyecto,$r,$claim_id)");
            }
        }

        $requerimientos = $this->ProyectRequirement->query("SELECT ProyectRequirement.`id` ,Requirement.texto, ProyectRequirement.`calificacion` FROM `proyect_requirements` ProyectRequirement LEFT JOIN requirements Requirement ON ProyectRequirement.`requirement_id` = Requirement.id WHERE ProyectRequirement.proyect_id=$proyecto AND ProyectRequirement.claim_id=$claim_id");
        $this->set('requisitos', $requerimientos);
    }

    function edit_reevaluation($id = null, $claim_id) {
        $this->set('est', $this->Session->read('estado'));
        $this->set('claim_id', $claim_id);
        if (empty($this->data)) {
            App::import('Model', 'RequirementFault');
            $ava = new RequirementFault();
            $ava->recursive = -1;
            $this->ProyectRequirement->recursive = 0;
            $this->data = $this->ProyectRequirement->find('first', array('conditions' => array('ProyectRequirement.id' => $id), 'fields' => array('ProyectRequirement.id', 'ProyectRequirement.calificacion', 'ProyectRequirement.requirement_id', 'Proyect.codigo', 'Requirement.texto')));
            $this->set('chequeados', $this->ProyectRequirement->ProyectRequirementsRequirementFault->findAllByProyectRequirementId($id));
            $this->set('condiciones', $ava->find('all', array('conditions' => array('RequirementFault.requirement_id' => $this->data['ProyectRequirement']['requirement_id']), 'order' => array('RequirementFault.tipo' => 'asc'), 'fields' => array('RequirementFault.id', 'RequirementFault.nombre', 'RequirementFault.tipo'))));
        } else {


            $datos = $this->params['form'];
            if ($this->ProyectRequirement->save($this->data)) {
                $ide = $this->data['ProyectRequirement']['id'];
                $this->ProyectRequirement->ProyectRequirementsRequirementFault->deleteAll(array('ProyectRequirementsRequirementFault.proyect_requirement_id' => $ide));
                $flag = 0;
                foreach ($datos['checked'] as $value) {
                    if ($value != 0) {
                        $flag = 1;
                        $this->ProyectRequirement->ProyectRequirementsRequirementFault->query("INSERT INTO proyect_requirements_requirement_faults (proyect_requirement_id,requirement_fault_id) values($ide,$value)");
                    }
                }
                if ($flag == 0) {
                    $this->ProyectRequirement->updateAll(array('ProyectRequirement.calificacion' => 0), array('ProyectRequirement.id' => $ide));
                }



                $this->Session->setFlash('Registro guardado exitosamente');
                $this->redirect(array('controller' => 'ProyectRequirements', 'action' => 'reevaluation_index', $claim_id));
            }
        }
    }

    function index() {
        $id = $this->Session->read('proy_id');
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } else {
            $this->layout = "ajax";

            $proyecto = $this->Session->read('proy_id');
            $call_id = $this->Session->read('call_id');
            $requerimientos = $this->ProyectRequirement->Requirement->find(
                    'all', array(
                'conditions' => array('Requirement.requirement_type_id' => 1, 'Requirement.call_id' => $call_id),
                'fields' => array('Requirement.id'),
                'recursive' => -1,
                    ));
            

            foreach ($requerimientos as $req) {
                $r = $req['Requirement']['id'];


                $cont = $this->ProyectRequirement->find('count', array('conditions' => array('requirement_id' => $req['Requirement']['id'], 'proyect_id' => $proyecto, 'claim_id' => 0)));
                if ($cont == 0) {
                    $this->ProyectRequirement->query("INSERT INTO proyect_requirements (proyect_id,requirement_id,claim_id)values($proyecto,$r,0)");
                }
            }



            if (empty($this->data)) {

                $requerimientos = $this->ProyectRequirement->find('all', array('recursive' => -1, 'conditions' => array('ProyectRequirement.proyect_id' => $id, 'ProyectRequirement.claim_id' => 0), 'fields' => array('Requirement.texto', 'ProyectRequirement.calificacion', 'ProyectRequirement.id'), 'joins' => array(array('table' => 'requirements', 'alias' => 'Requirement', 'type' => 'left', 'conditions' => array('Requirement.id=ProyectRequirement.requirement_id')))));
                $this->set('requisitos', $requerimientos);
            }
        }
    }

}

?>
